package com.pw.study.flink.transform;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoFlatMapFunction;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;
import org.apache.flink.util.Collector;

public class TransFormConnect {
    public static void main(String[] args) {
        dealConnect();
    }


    private static void dealConnect() {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(3);
        DataStreamSource<Integer> stream1 = env.fromElements(1, 2, 3, 4, 5, 6);
        DataStreamSource<Integer> stream2 = env.fromElements(11, 22, 33, 44, 55, 66);
        ConnectedStreams<Integer, Integer> ct = stream1.connect(stream2);
        stream1.connect(stream2).map(new CoMapFunction<Integer, Integer, String>() {

            @Override
            public String map1(Integer in) throws Exception {
                System.out.println("map1: "+in);
                return null;
            }

            @Override
            public String map2(Integer in2) throws Exception {
                System.out.println("map2: "+in2);
                return null;
            }
        });
        stream1.connect(stream2).flatMap(new CoFlatMapFunction<Integer, Integer, String>() {

            @Override
            public void flatMap1(Integer integer, Collector<String> collector) throws Exception {
                System.out.println("ma1:.....");
            }

            @Override
            public void flatMap2(Integer integer, Collector<String> collector) throws Exception {
                System.out.println("coll....");
            }
        });
        ct.getFirstInput().print("first");
        ct.getSecondInput().print("second");

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
